package org.dromara.work.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.dromara.common.core.service.DictService;

import org.dromara.work.domain.SpatialTunnel;
import org.dromara.work.mapper.SpatialTunnelMapper;
import org.dromara.work.service.ISpatialAttrBaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 池塘、海塘属性信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-20
 */
@RequiredArgsConstructor
@Service
public class ISpatialTunnelServiceImpl implements ISpatialAttrBaseService {

    private final SpatialTunnelMapper baseMapper;
    private final DictService dictService;
    @Override
    public List<String> getDmlb() {
        //“temp-”用于区分临时库和现状库
        String[] array = {"base-23620"};
        return (List<String>) Convert.toList(array);
    }


    //基准库只能修改
    @Override
    public void saveAttribute(Map<String, String> map) {
        String data = map.get("data");
        String code = map.get("code");
        SpatialTunnel obj = JSONObject.parseObject(data, SpatialTunnel.class);
        obj.setId(null);
        obj.setCode(code);
        baseMapper.insert(obj);

    }

    @Override
    public void updateAttribute(Map<String, String> map) {
        String data = map.get("data");
        String code = map.get("code");
        SpatialTunnel obj = JSONObject.parseObject(data, SpatialTunnel.class);
        SpatialTunnel exist = baseMapper.selectOne(new LambdaQueryWrapper<SpatialTunnel>().eq(SpatialTunnel::getCode, code));
        if (null != exist) {
            obj.setCode(code);
            obj.setId(exist.getId());
            baseMapper.updateById(obj);
        } else {
            baseMapper.insert(obj);
        }
    }

    @Override
    public void delAttribute(String id) {

    }

    @Override
    public JSONObject getAttributeByCode(String id) {
        SpatialTunnel obj = baseMapper.selectOne(new LambdaQueryWrapper<SpatialTunnel>().eq(SpatialTunnel::getCode, id));
        if (null != obj) {
            return BeanUtil.toBean(obj,JSONObject.class);
        }
        return null;
    }

    @Override
    public JSONObject getAttributeLabelByCode(String id) {
        SpatialTunnel obj = baseMapper.selectOne(new LambdaQueryWrapper<SpatialTunnel>().eq(SpatialTunnel::getCode, id));
        if (null != obj) {
            String level= dictService.getDictLabel("bridge_type",obj.getType());
            obj.setType(level);
            return BeanUtil.toBean(obj,JSONObject.class);
        }
        return null;
    }

}
